package common.test;

import common.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 98. 验证二叉搜索树
 */
public class _98_IsValidBST {

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return false;
        }
        TreeNode pre = null;
        Deque<TreeNode> stack = new LinkedList<>();
        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            TreeNode node = stack.pop();
            if (pre != null && pre.val >= node.val) {
                return false;
            }
            pre = node;
            root = node.right;
        }
        return true;
    }
}
